package com.lionxing.DNTG;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;

import org.cocos2d.nodes.CCDirector;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.MediaStore;

public class filemanager extends Object {

	
	private HashMap<String, Object> leveinfo;
	
	private HashMap<String, Object> gameinfo;
	
	public final static String path = "data/data/com.lionxing.DNTG";
	
	private Context _filecontext;
	
    private static filemanager _sharedDirector = new filemanager();

    /** returns a shared instance of the director */
    public static filemanager sharedDirector() {
      
            return _sharedDirector;

		//
		// Default Director is TimerDirector

    }
    
    protected filemanager()
    {
    	
    	loadleveinfo();
    	loadgameinfo();
    }
    
    
    public void saveHashMaptofile()
    {
    	
    	if( leveinfo == null)
    	{
    		
    		return;
    	}
    	
    	
    	File file = new File(path, "mm.dat");
    	
    	
    	try{
    		
    		FileOutputStream outstream = new FileOutputStream(file);
    		ObjectOutputStream objectoutputstream = new ObjectOutputStream(outstream);
    		
    		objectoutputstream.writeObject(leveinfo);
    		outstream.close();
    		
    		
    	}catch (FileNotFoundException e){
			e.printStackTrace();
			
		}catch (IOException e)
		{
			e.printStackTrace();

		}
    	
    }
    
    public HashMap<String, Object> getHashmapmmdat()
    {
    	
    	return leveinfo;
    }
    
    public HashMap<String, Object> getHashmapgameinfo()
    {
    	
    	return gameinfo;
    }
    
    
    private void loadleveinfo()
    {
    	File file = new File(path,"mm.dat");
    	
    	leveinfo = new HashMap<String, Object>();
    	
    	if(!file.exists() || file.length() == 0)
    	{
    		
    		
    		for(int i = 1 ; i < 73 ; i++ )
    		{
    			String filename = String.format("%d.png", i);
    			leveinfo.put(filename, false);
    			//leveinfo.put(filename+"step", 0);
    		}
    	}
    	else 
    	{
    		
    		FileInputStream  freader =null;
    		
    		try{
    			
    			freader = new FileInputStream(file);
    			
    			ObjectInputStream objectinputstream = new ObjectInputStream(freader);
    			
    			leveinfo = (HashMap<String, Object>)objectinputstream.readObject();
    			
    			freader.close();
    			
    		} catch (FileNotFoundException e){
    			e.printStackTrace();
    			
    		}catch (IOException e)
    		{
    			e.printStackTrace();

    		}catch (ClassNotFoundException e)
    		{
    			e.printStackTrace();

    		}
    	}
    	
    	if(leveinfo == null)
    	{
    		for(int i = 1 ; i < 73 ; i++ )
    		{
    			String filename = String.format("%d.png", i);
    			leveinfo.put(filename, false);
    			//leveinfo.put(filename+"step", 0);
    		}
    	}
    	
    }
    
    private void loadgameinfo()
    {
    	File file = new File(path,"game.dat");
    	gameinfo =new HashMap<String, Object>();
    	
    	if(!file.exists() || file.length() == 0)
    	{
    		
    		gameinfo.put("bgsound", 50.0f);
    		gameinfo.put("effectsound", 50.0f);
    		gameinfo.put("bookinfo", 1);

    	}
    	else
    	{
    		FileInputStream  freader =null;
    		
    		try{
    			
    			freader = new FileInputStream(file);
    			
    			ObjectInputStream objectinputstream = new ObjectInputStream(freader);
    			
    			gameinfo = (HashMap<String, Object>)objectinputstream.readObject();
    			
    			freader.close();
    			
    		} catch (FileNotFoundException e){
    			e.printStackTrace();
    			
    		}catch (IOException e)
    		{
    			e.printStackTrace();

    		}catch (ClassNotFoundException e)
    		{
    			e.printStackTrace();

    		}
    		
    	}
    	
    }
    
    public void savegameinfo()
    {
    	
    	if( gameinfo == null)
    	{
    		
    		return;
    	}
    	
    	
    	File file = new File(path, "game.dat");
    	
    	
    	try{
    		
    		FileOutputStream outstream = new FileOutputStream(file);
    		ObjectOutputStream objectoutputstream = new ObjectOutputStream(outstream);
    		
    		objectoutputstream.writeObject(gameinfo);
    		outstream.close();
    		
    		
    	}catch (FileNotFoundException e){
			e.printStackTrace();
			
		}catch (IOException e)
		{
			e.printStackTrace();

		}
    }
    
    
    public void savepictoSD(int pic)
    {
    	if(pic < 0)
    	{
    		pic = 1;
    	}
    	
    	if(pic > 72)
    	{
    		pic = 72;
    	}
    	

    	String name2 = String.format("%d.png", pic);

	
		Bitmap bmp = null;
		try {

			bmp = BitmapFactory.decodeStream(_filecontext.getAssets().open(name2));

			} catch (Exception e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

			}
			
			
			if(bmp != null)
			{
				
					MediaStore.Images.Media.insertImage(_filecontext.getContentResolver(), bmp, name2, "DNTG");
		
			}


    }
    
    public void setContext(Context context)
    {
    	_filecontext = context;
    }

    
    public boolean isSDexist()
    {
    	
    	return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    }
}
